package study.thread.basic;

/*
 * join方法让一个线程等待另一个线程的完成
 */
public class JoinTest extends Thread {
	
	static int count = 0;
	
	
	public JoinTest(String name) {
		super(name);
	}
	
	public void run() {
		System.out.println(this.getName()+" 开始计算");
		try {
			//让当前执行的线程睡眠4000毫秒
			Thread.sleep(4000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		count = (int) (Math.random()*1000);
		
		System.out.println(this.getName()+" 结束计算");
	}

	public static void main(String[] args) throws InterruptedException {
		System.out.println("==========《主线程执行》==========");
		Thread thread = new JoinTest("计算线程");
		
		thread.start();
		
		long start = System.currentTimeMillis();
		//在主线程里面调用计算线程的join方法，则主线程暂停执行，直到计算线程终止
//		thread.join();
		
		long end = System.currentTimeMillis();
		
		System.out.println(end-start+" ms 后 count = "+count);
		System.out.println("==========《主线程退出》==========");
	}

}
